package com.yixing.demo.interceptor;

import com.yixing.demo.constants.Constants;
import com.yixing.demo.model.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User userInfo = (User)session.getAttribute(Constants.USER_SESSION_KEY);
        if(userInfo == null || userInfo.getId() < 1) {
            return false;
        }
        return true;
    }
}
